<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 每日数据
                </el-breadcrumb-item>
                <el-button style="margin-left: 90%"
                           type="text"
                           icon="el-icon-refresh"
                           @click="getData"
                >刷新
                </el-button>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除
                </el-button>
                <el-date-picker @change="changeDate"
                                size="small"
                                v-model="vDate"
                                type="date"
                                placeholder="选择日期">
                </el-date-picker>

                <el-button type="primary" icon="el-icon-plus" style="margin-left: 1%" @click="handleAdd">添加吐司销售
                </el-button>
            </div>
            <el-table
                show-summary
                :summary-method="getSummaries"
                stripe
                :data="productRecodeTable"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center">
                    <template slot-scope="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column prop="categoryName" label="产品分类" align="center"></el-table-column>
                <el-table-column prop="productName" label="产品名称" align="center"></el-table-column>
                <el-table-column prop="productionNum" label="生产数量" align="center"></el-table-column>
                <el-table-column prop="salesNum" label="销售数量" align="center"></el-table-column>
                <el-table-column prop="failedNum" label="报损数量" align="center"></el-table-column>
                <el-table-column prop="testEatNum" label="试吃数量" align="center"></el-table-column>
                <el-table-column prop="giveNum" label="赠送数量" align="center"></el-table-column>
                <el-table-column prop="residueNum" label="剩余数量" align="center"></el-table-column>
                <el-table-column prop="date" label="日期" width="120" align="center">
                    <template slot-scope="scope">{{ scope.row.date| dateFormat }}</template>
                </el-table-column>
                <el-table-column prop="updTime" label="更新时间" width="160" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.row.id,scope.row.productName)"
                        >编辑
                        </el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row.id)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!--添加销量弹窗-->
        <el-drawer

            :visible.sync="drawer"
            :with-header="false" size="25%">
            <div style="margin-top: 10%">
                <div style="margin-left: 45%">
                    <h3 style="color: #606266">{{ rightTitle }}</h3>
                </div>

                <el-form status-icon :model="rightForm" ref="ruleForm" label-width="80px" class="demo-ruleForm"
                         style="margin-left: 8%;margin-top: 10%">

                    <el-form-item label="生产数量" prop="pass" style="margin-top: 6%">
                        <el-input type="text" @keyup.enter.native="saveRightForm" @keyup.esc.native="saveSalesForm"
                                  v-model="rightForm.productionNum" autocomplete="off" style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="售出数量" prop="pass" style="margin-top: 6%">
                        <el-input type="text" @keyup.enter.native="saveRightForm" @keyup.esc.native="saveSalesForm"
                                  v-model="rightForm.salesNum" autocomplete="off" style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="报废数量" prop="pass" style="margin-top: 6%">
                        <el-input type="text" @keyup.enter.native="saveRightForm" @keyup.esc.native="saveSalesForm"
                                  v-model="rightForm.failedNum" autocomplete="off" style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="试吃数量" prop="pass" style="margin-top: 6%">
                        <el-input type="text" @keyup.enter.native="saveRightForm" @keyup.esc.native="saveSalesForm"
                                  v-model="rightForm.testEatNum" autocomplete="off" style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="赠送数量" prop="pass" style="margin-top: 6%">
                        <el-input type="text" @keyup.enter.native="saveRightForm" @keyup.esc.native="saveSalesForm"
                                  v-model="rightForm.giveNum" autocomplete="off" style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="销量时间">
                        <el-date-picker
                            type="date"
                            placeholder="选择日期"
                            v-model="rightForm.date"
                            value-format="yyyy-MM-dd"
                            style="width: 80%;">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item style="margin-left: 40%;margin-top: 8%">
                        <el-button @click="cancelRightFrom">取消</el-button>
                        <el-button type="primary" @click="saveRightForm">提交</el-button>
                    </el-form-item>
                </el-form>
            </div>

        </el-drawer>
    </div>
</template>

<script>
import {
    downloadOfficeTable,
    updateOfficeTable,
    getJobState,
    getOfficeTableInfo,
    delOfficeTable,
    addPersonalOfficeTable
} from '@/api/job';
import { getAllUser } from '@/api/staff';
import {
    delProductRecode,
    addUpdateProductRecode,
    getProductRecodeInfo,
    getLisProductRecode
} from '@/api/productRecode';
import format from '@/utils/format';

export default {
    name: 'basetable',
    data() {
        return {
            rightForm: {},
            rightTitle: '',
            vDate: new Date(),
            query: {
                date: new Date(),
                pageIndex: 1,
                pageSize: 90
            },
            productRecodeTable: [],
            multipleSelection: [],
            drawer: false,
            pageTotal: 0,
            ids: {
                ids: []
            }
        };
    },
    created() {
        this.getData();
        this.getDataRoute();
    },
    watch: {
        '$route': 'getDataRoute'
    },

    methods: {

        handleAdd() {
            this.$router.push({
                name: 'product'
            });

        },

        //表格合计计算
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 2) {
                    sums[index] = '合计数量：';
                    return;
                }
                if (index === 1) {
                    sums[index] = '';
                    return;
                }
                if (index === 3) {
                    sums[index] = '（单位/个）';
                    return;
                }
                if (index === 12) {
                    sums[index] = format(this.query.date, 'YYYY 年 MM 月 DD 日');
                    return;
                }
                const values = data.map(item => Number(item[column.property]));
                if (!values.every(value => isNaN(value))) {
                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                    }, 0);
                    sums[index] += ' 个';
                } else {
                    sums[index] = ' ';
                }
            });

            return sums;
        },


        //日期选择框改变事件
        changeDate() {
            this.getData();

        },
        cancelRightFrom() {
            this.drawer = false;
            this.rightForm = {
                date: new Date()
            };

        },
        //编辑弹窗
        handleEdit(id, titleName) {
            this.rightTitle = titleName;
            getProductRecodeInfo(id).then(res => {
                if (res.success) {
                    this.rightForm = res.data;
                    this.drawer = true;
                    console.log(this.rightForm);
                } else {
                    this.$message.error(res.message);
                }

            });
        },

        //修改右弹窗数据
        saveRightForm() {
            addUpdateProductRecode(this.rightForm).then(res => {
                if (res.success) {
                    this.cancelRightFrom();
                    this.$message.success('修改成功');
                    this.getData();
                } else {
                    this.$message.warning(res.message);
                }

            });

        },

        //路由获取列表
        getDataRoute() {
            console.log('路由成功跳转');
            const date = this.$route.query.date;
            console.log(this.$route.query.date);
            if (date === undefined) {
                return;
            }
            console.log(date);
            this.vDate = date;
            this.query.date = date;
            getLisProductRecode(this.query).then(res => {
                this.productRecodeTable = res.data.records;
                this.pageTotal = res.data.total || 0;

            });
        },

        // 获取列表
        getData() {
            this.query.date = this.vDate;
            getLisProductRecode(this.query).then(res => {
                if (res.success) {
                    this.productRecodeTable = res.data.records;
                    this.pageTotal = res.data.total || 0;
                } else {
                    this.$message.warning(res.message);
                    if (res.code === 10005) {
                        this.$router.push({ path: '/login' });
                    }
                }

            });
        },
        //取消按钮
        cancel() {
            this.addVisible = false;
            //清空对象
            this.tableData = {};
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(userId) {
            if (userId !== undefined) {
                this.ids.ids = [];
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                this.ids.ids.push(userId);
                delProductRecode(this.ids).then(res => {
                    if (res.success) {
                        this.$message.success('删除成功');
                        this.getData();
                    } else {
                        this.$message.error(res.message);
                    }

                });
            }).catch(() => {
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            for (let i = 0; i < this.multipleSelection.length; i++) {
                this.ids.ids.push(this.multipleSelection[i].id);

            }
            this.handleDelete();


        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>

.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.everyDay {
    display: inline-block;
    width: 20px;
    height: 20px;
    background-color: #409eff;
    color: #fff;
    border-radius: 50%;
}
</style>
